﻿//创建时间: 2010-10-29
//创建作者: 李晓忠
//功能说明: CMS系统 页面发布
//修改: 2010-11-22 李城 完成代码的编写 
using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using TRPOP.CMS.Web.UI;
using TRPOP.CMS.Model;
using TRPOP.CMS.Business;
using TRPOP.Common;
using TRPOP.Config;


namespace TRPOP.CMS.Web.Admin
{
    public partial class admin_publish : BaseAdmin
    {

        #region 当前页面权限验证
        protected override void SubPageInit()
        {
            base.m_SubPagePermission = Plugin.PermissionKeys.PermissionEnum.Content_AllControl;
        }
        #endregion

        #region 页面变量
        private ContentManage m_ContentManage = new ContentManage();
        #endregion

        #region 页面事件


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadBase();
            }
        }

        /// 返回操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if ( ViewState["RequestUrl"] == null 
                || ViewState["RequestUrl"].ToString().ToLower().Contains("lefttree.aspx")
                || ViewState["RequestUrl"].ToString().ToLower().Contains("admin_publish.aspx")
                )
            {
                base.Redirect("../Admin_Main.aspx");
            }
            else
                base.Redirect(ViewState["RequestUrl"].ToString());
        }

        #endregion

        #region 页面方法

        /// <summary>
        /// 操作导航
        /// </summary>
        /// <summary>
        public void LoadBase()
        {
            try
            {
                ViewState["RequestUrl"] = Request.UrlReferrer.ToString();
            }
            catch { }

            if (TRPOP.Common.RequestHelper.RequestString("refer").IsEmpty() == false)
            {
                ViewState["RequestUrl"] = TRPOP.Common.RequestHelper.RequestString("refer");
            }

            if (TRPOP.Common.RequestHelper.RequestString("ac").IsEmpty() == false)
            {
                #region
                string strNodeids = TRPOP.Common.RequestHelper.RequestString("nodeid").Trim();//所有操作的结点
                int intOnceOper = 2;//单次操作数量
                string strContentids = TRPOP.Common.RequestHelper.RequestString("contentid").Trim();//所有操作的内容ids
                string strAcType = TRPOP.Common.RequestHelper.RequestString("ac").Trim();//操作类型
                string strExtraids = "";
                int intContentTotal = 0;//一共操作的内容的数量 为结点发布使用
                foreach (string strTemp in strContentids.Split(','))
                {
                    if (strTemp.IsEmpty() == false)
                        intContentTotal++;
                }

                bool blnNode = false;
                bool blnContent = false;
                bool blnExtra = false;

                int intOnceOperTemp = TRPOP.Common.RequestHelper.RequestString("onceOper").Trim().ToInt();
                intOnceOper = intOnceOperTemp > 0 ? intOnceOperTemp : intOnceOper;

                switch (strAcType.Trim().ToLower())
                {
                    #region 根据传递的参数导航到不同的函数进行执行
                    case "pubcontent"://只发布内容
                        blnContent = true;
                        break;
                    case "pubnode"://只发布结点首页
                        blnNode = true;
                        break;
                    case "puball"://发布内容、结点首页
                        blnNode = true;
                        blnContent = true;
                        break;
                    case "repubcontent"://只重新（更新）发布内容
                        blnContent = true;
                        strContentids = GetContentIds(strContentids, strNodeids, PubStatus.EnumElement.Publish);
                        break;
                    case "repubnode"://只重新（更新）发布结点首页
                        blnNode = true;
                        break;
                    case "repuball"://重新（更新）发布内容、结点首页]
                        blnNode = true;
                        blnContent = true;
                        strContentids = GetContentIds(strContentids, strNodeids, PubStatus.EnumElement.Publish);
                        break;
                    case "concelpubnode"://取消发布结点首页
                        Concelnodes(strNodeids);
                        break;
                    case "concelpubcontent"://取消发布内容
                        ConcelContents(strContentids);
                        break;
                    case "concelpuball"://取消发布内容、结点首页
                        strContentids = GetContentIds(strContentids, strNodeids, PubStatus.EnumElement.Publish);
                        Concelnodes(strNodeids);
                        ConcelContents(strContentids);
                        break;
                    case "noderefresh"://站点更新
                        #region
                        if (TRPOP.Common.RequestHelper.RequestString("include_sub").Trim() == "1")
                        {
                            if (strNodeids.Contains(",") == false)
                            {
                                string strnodeidNew = "";
                                List<NodeInfoWithLevel> list = new NodeManage().GetNodeListTree(strNodeids.ToInt(), 0, "");
                                foreach (NodeInfoWithLevel oinfo in list)
                                {
                                    strnodeidNew += oinfo.NodeId.ToString() + ",";
                                }
                                if (strnodeidNew.Length > 0)
                                {
                                    strnodeidNew = StringExtension.ClearLastChar(strnodeidNew);
                                    strNodeids = strnodeidNew;
                                }
                            }
                        }
                        blnNode = (TRPOP.Common.RequestHelper.RequestString("refresh_index").Trim() == "1");
                        blnContent = (TRPOP.Common.RequestHelper.RequestString("refresh_content").Trim() == "1");
                        strContentids = GetContentIds("", strNodeids, PubStatus.EnumElement.Publish);
                        blnExtra = (TRPOP.Common.RequestHelper.RequestString("refresh_extra").Trim() == "1");
                        if(blnExtra)
                            strExtraids = GetExtraids(TRPOP.Common.RequestHelper.RequestString("nodeid").Trim());
                        int intonceOperTemp = 0;
                        int.TryParse(TRPOP.Common.RequestHelper.RequestString("onceOper").Trim(), out intonceOperTemp);
                        if (intonceOperTemp > 0)
                            intOnceOper = intonceOperTemp;
                        #endregion
                        break;
                    case "nodepub"://结点发布
                        #region
                        if (TRPOP.Common.RequestHelper.RequestString("include_sub").Trim() == "1")
                        {
                            if (strNodeids.Contains(",") == false)
                            {
                                string strnodeidNew = "";
                                List<NodeInfoWithLevel> list = new NodeManage().GetNodeListTree(strNodeids.ToInt(), 0, "");
                                foreach (NodeInfoWithLevel oinfo in list)
                                {
                                    strnodeidNew += oinfo.NodeId.ToString() + ",";
                                }
                                if (strnodeidNew.Length > 0)
                                {
                                    strnodeidNew = StringExtension.ClearLastChar(strnodeidNew);
                                    strNodeids = strnodeidNew;
                                }
                            }
                        }
                        blnNode = true;
                        blnContent = true;
                        strContentids = GetContentIds("", strNodeids, PubStatus.EnumElement.UnPublish);
                        #endregion
                        break;
                    case "refreshextra"://更新附加发布
                        #region
                        if (TRPOP.Common.RequestHelper.RequestString("NodeAddionalPubId").Trim().IsEmpty() == false)
                        {
                            strExtraids = TRPOP.Common.RequestHelper.RequestString("NodeAddionalPubId").Trim();
                            blnExtra = true;
                        }
                        #endregion
                        break;
                    case "refreshextraall"://更新所有附加发布
                        #region
                        if (TRPOP.Common.RequestHelper.RequestString("nodeid").Trim().IsEmpty()==false)
                        {
                            strExtraids = GetExtraids(TRPOP.Common.RequestHelper.RequestString("nodeid").Trim());
                            blnExtra = true;
                        }
                        #endregion
                        break;
                    case "allnodepub"://整站发布
                        #region
                        if (TRPOP.Common.RequestHelper.RequestString("include_sub").Trim() == "1")
                        {
                            string strnodeidNew = "";
                            List<NodeInfoWithLevel> list = new NodeManage().GetNodeListTree(strNodeids.ToInt(), 0, "");
                            foreach (NodeInfoWithLevel oinfo in list)
                            {
                                strnodeidNew += oinfo.NodeId.ToString() + ",";
                            }
                            if (strnodeidNew.Length > 0)
                            {
                                strnodeidNew = StringExtension.ClearLastChar(strnodeidNew);
                                strNodeids = strnodeidNew;
                            }
                        }
                        else
                        {
                            string strnodeidNew = "";
                            List<NodeInfo> list = new NodeManage().GetListByCondition(0, " and ParentId=0 ", "");
                            foreach (NodeInfo oinfo in list)
                            {
                                strnodeidNew += oinfo.NodeId.ToString() + ",";
                            }
                            if (strnodeidNew.Length > 0)
                            {
                                strnodeidNew = StringExtension.ClearLastChar(strnodeidNew);
                                strNodeids = strnodeidNew;
                            }
                        }
                        strContentids = GetContentIds("", strNodeids, PubStatus.EnumElement.UnPublish);
                        blnContent = true;
                        #endregion
                        break;
                    case "allnoderefresh"://整站更新
                        #region
                        if (TRPOP.Common.RequestHelper.RequestString("include_sub").Trim() == "1")
                        {
                            string strnodeidNew = "";
                            List<NodeInfoWithLevel> list = new NodeManage().GetNodeListTree(strNodeids.ToInt(), 0, "");
                            foreach (NodeInfoWithLevel oinfo in list)
                            {
                                strnodeidNew += oinfo.NodeId.ToString() + ",";
                            }
                            if (strnodeidNew.Length > 0)
                            {
                                strnodeidNew = StringExtension.ClearLastChar(strnodeidNew);
                                strNodeids = strnodeidNew;
                            }
                        }
                        else
                        {
                            string strnodeidNew = "";
                            List<NodeInfo> list = new NodeManage().GetListByCondition(0, " and ParentId=0 ", "");
                            foreach (NodeInfo oinfo in list)
                            {
                                strnodeidNew += oinfo.NodeId.ToString() + ",";
                            }
                            if (strnodeidNew.Length > 0)
                            {
                                strnodeidNew = StringExtension.ClearLastChar(strnodeidNew);
                                strNodeids = strnodeidNew;
                            }
                        }
                        blnNode = (TRPOP.Common.RequestHelper.RequestString("refresh_index").Trim() == "1");
                        blnContent = (TRPOP.Common.RequestHelper.RequestString("refresh_content").Trim() == "1");
                        strContentids = GetContentIds("", strNodeids, PubStatus.EnumElement.Publish);
                        blnExtra = (TRPOP.Common.RequestHelper.RequestString("refresh_extra").Trim() == "1");
                        #endregion
                        break;
                    default:
                        break;
                    #endregion
                }
                if (blnNode == false)
                    spaceusedNode.InnerText = "无";
                if (blnExtra == false)
                    spaceusedExtraPub.InnerText = "无";
                if (blnContent == false)
                    spaceusedContent.InnerHtml = "无";
                if (blnExtra || blnNode)
                {
                    intContentTotal = new ContentManage().GetContentCount(strNodeids,
                        string.Format("{0},{1}", (int)PubStatus.EnumElement.UnPublish, (int)PubStatus.EnumElement.Publish)
                        );
                }
                ltJS.Text = string.Format(ltJS.Text, new object[] { 
                                                                    strNodeids,
                                                                    intOnceOper,     
                                                                    strContentids,
                                                                    strAcType,
                                                                    intContentTotal,
                                                                    strExtraids,
                                                                    blnNode.ToString().ToLower(),
                                                                    blnContent.ToString().ToLower(),
                                                                    blnExtra.ToString().ToLower(),
                                                                    SYSConfig.SystemVar_listPageCount
                                                                    }
                                          );
                #endregion
            }
            else if (TRPOP.Common.RequestHelper.RequestString("AjaxPostBack").IsEmpty() == false)
            {
                DoAjaxPostBack();
            }
            else
                ltJS.Text = "";
        }

        //异步操作
        public void DoAjaxPostBack()
        {
            Response.ClearHeaders();
            Response.ClearContent();
            string strType = TRPOP.Common.RequestHelper.RequestString("type");
            string strNodeIds = TRPOP.Common.RequestHelper.RequestString("nodeIds");
            string strContentids = TRPOP.Common.RequestHelper.RequestString("contentids");
            string strExtras = TRPOP.Common.RequestHelper.RequestString("Extras");
            switch (strType)
            {
                case "node":
                    Publish.PublishHtml.PubNode(strNodeIds, TRPOP.Common.RequestHelper.RequestInt("index"), TRPOP.Common.RequestHelper.RequestInt("countNum"));
                    Response.Write("OK");
                    base.End();
                    break;
                case "content":
                    Publish.PublishHtml.PubContent(strContentids);
                    Response.Write("OK");
                    base.End();
                    break;
                case "Extra":
                    Publish.PublishHtml.PubExtra(strExtras, TRPOP.Common.RequestHelper.RequestInt("index"), TRPOP.Common.RequestHelper.RequestInt("countNum"));
                    Response.Write("OK");
                    base.End();
                    break;
                default:
                    break;
            }
        }
       

        /// <summary>
        /// contentid的二次处理，是否反查结点下的所有信息
        /// </summary>
        /// <param name="strContentids"></param>
        /// <param name="strNodeids"></param>
        /// <param name="EnumPublish"></param>
        /// <returns></returns>
        public string GetContentIds(string strContentids, string strNodeids, PubStatus.EnumElement EnumPublish)
        {
            bool nodeFlash = true;//结点下的内容更新

            if (strNodeids.IsEmpty() || strContentids.IsEmpty() == false)
                nodeFlash = false;
            else if (strContentids.Trim().IsEmpty())
                    nodeFlash = true;
            if (nodeFlash)//如果结点更新
            {
                string strWhere = string.Format("and  NodeId in  ({0}) and PubStatus={1}", strNodeids, (int)EnumPublish);
                List<ContentInfo> olist = (new ContentManage()).GetListByCondition(0, strWhere, "");
                strContentids = "";
                foreach (ContentInfo oInfo in olist)
                {
                    strContentids += oInfo.ContentId.ToString() + ",";
                }
                if (strContentids.Length >= 1)
                    strContentids = strContentids.Remove(strContentids.Length - 1);
            }
            else
            {
                string strWhere = string.Format("and  contentid in  ({0}) and PubStatus={1}", strContentids, (int)EnumPublish);
                List<ContentInfo> olist = (new ContentManage()).GetListByCondition(0, strWhere, "");
                strContentids = "";
                foreach (ContentInfo oInfo in olist)
                {
                    strContentids += oInfo.ContentId.ToString() + ",";
                }
                if (strContentids.Length >= 1)
                    strContentids = strContentids.Remove(strContentids.Length - 1);
            }

            #region 虚链接Id
            if (strContentids != "")
            {
                string strVirtualIds = "";
                List<ContentInfo> vlist = m_ContentManage.GetVirtualLink(strContentids);
                foreach (ContentInfo oInfo in vlist)
                {
                    strVirtualIds += oInfo.ContentId.ToString() + ",";
                }
                if (strVirtualIds.Length >= 1)
                    strVirtualIds = strVirtualIds.Remove(strVirtualIds.Length - 1);
                if (strVirtualIds != "")
                {
                    strContentids = strContentids + "," + strVirtualIds;
                }
            }
            #endregion

            return strContentids;
        }

        /// <summary>
        /// 取消发布结点
        /// </summary>
        /// <param name="strNodeids"></param>
        public void Concelnodes(string strNodeids)
        {
            int intNodeid = 0;
            NodeInfo oNodeInfo = new NodeInfo();
            NodeManage oNodeManage = new NodeManage();
            foreach (string strTemp in strNodeids.Split(','))
            {
                int.TryParse(strTemp, out intNodeid);
                if (intNodeid > 0)
                {
                    oNodeInfo = oNodeManage.GetInfoById(intNodeid);
                    if (oNodeInfo != null)
                    {
                       string strpath=  (new PSNManage()).GetPSNPathByPSN(oNodeInfo.HtmlPSN);
                       strpath = strpath + "/" + oNodeInfo.IndexName;
                       strpath = strpath.Replace("//","/");
                       FileUtil.DeleteFile(strpath, true, "_");//删除文件
                    }
                }
            }
        }

        /// <summary>
        /// 取消发布 内容
        /// </summary>
        /// <param name="strContentids"></param>
        public void ConcelContents(string strContentids)
        {
            int intContentid = 0;
            ContentInfo oContentInfo = new ContentInfo();
            ContentManage oContentManage = new ContentManage();
            foreach (string strTemp in strContentids.Split(','))
            {
                int.TryParse(strTemp, out intContentid);
                if (intContentid > 0)
                {
                    oContentInfo = oContentManage.GetInfoById(intContentid);
                    if (oContentInfo != null)
                    {
                        FileUtil.DeleteFile(oContentInfo.Url, true, "_");//删除文件
                        oContentInfo.PubStatus = (int)PubStatus.EnumElement.UnPublish;
                        oContentManage.Save(oContentInfo);
                    }
                }
            }
        }

        /// <summary>
        /// 根据结点找到所有的附加发布
        /// </summary>
        /// <param name="strNodeid"></param>
        /// <returns></returns>
        public string GetExtraids(string strNodeid)
        {
            int intNodeid = 0;
            int.TryParse(strNodeid, out intNodeid);
            if (intNodeid <= 0)
                return "";
            else
            {
                NodeAddionalPubManage oNodeAddionalPubManage = new NodeAddionalPubManage();
                List<NodeAddionalPubInfo> olist = oNodeAddionalPubManage.GetListByCondition(0, " and  NodeId=" + intNodeid.ToString() + " ", "");
                if (olist == null || olist.Count <= 0)
                    return "";
                string strReturn = "";
                foreach (NodeAddionalPubInfo oTemp in olist)
                    strReturn += oTemp.NodeAddionalPubId.ToString()+",";
                if (strReturn.Length > 0)
                    strReturn = strReturn.Remove(strReturn.LastIndexOf(','));
                return strReturn;
            }
        }

        #endregion
    }
}
